const scheme = {
  namespace: 'scheme',
  state: {
    schemeData: {},
    schemeIndex: 1,
    scenesView: {},
    sketchViewModel: {},
    isVisibleGraph: false,
  },
  effects: {
    *addData({ payload }, { call, select, put }) {
      const { schemeList } = payload;
      const schemeData = yield select(store => store.scheme.schemeData);
      let Data = {};
      if (schemeData) {
        Data = Object.assign({}, schemeData);
      }
      let schemeIndex = Object.keys(Data).length + 1;
      console.log('addData-schemeIndex', schemeIndex);
      let key = '方案' + schemeIndex;
      if (schemeList.length > 0) {
        Data[key] = schemeList;
      }
      yield put({
        type: 'updateData',
        payload: Data,
      });
      yield put({
        type: 'updateIndex',
        payload: schemeIndex,
      });
    },
    *delData({ payload }, { call, select, put }) {
      const { uid } = payload;
      const schemeData = yield select(store => store.scheme.schemeData);
      let Data = Object.assign({}, schemeData);
      Object.keys(Data).map(key => {
        Data[key] = Data[key].filter(i => i.uid !== uid);
        if (Data[key].length == 0) delete Data[key];
      });
      console.log('delData', Data);
      yield put({
        type: 'updateData',
        payload: Data,
      });
    },
  },
  reducers: {
    updateData(state, { payload }) {
      return { ...state, schemeData: payload };
    },
    updateIndex(state, { payload }) {
      return { ...state, schemeIndex: payload };
    },
    updateScenesView(state, { payload }) {
      return { ...state, scenesView: payload };
    },
    updateSketchViewModel(state, { payload }) {
      return { ...state, sketchViewModel: payload };
    },
    updateIsVisibleGraph(state, { payload }) {
      return { ...state, isVisibleGraph: payload };
    },
  },
};

export default scheme;
